package com.project.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.project.bean.GradeBean;
import com.project.bean.RoleBean;
import com.project.bean.UserBean;
import com.project.service.IRoleService;
import com.project.service.IUserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("user")
public class UserController {
    @Autowired
    private IUserService  userService;
    @Autowired
    private IRoleService roleService;
//    @RequiresRoles({"manager"})
    @RequestMapping("login")
    public UserBean login(String username, String password, HttpServletRequest request){
//        UserBean userBean = userService.login(username, password);
//        request.getSession().setAttribute("loginUser", userBean);

        //得到主题
        Subject subject = SecurityUtils.getSubject();
        //登陆完成后，shiro用户会将登录用户绑定在session中
        subject.login(new UsernamePasswordToken(username,password));
        //得到登录用户
        UserBean userBean1 = (UserBean)subject.getPrincipal();
        request.getSession().setAttribute("loginUser", userBean1);

        if(userBean1 != null){
            return userBean1;
        }
        else {
            return null;
        }

//        try {
//            if (userBean1.getUserName().equals("aaa")) {
//                return "successA";
//            }
//            else if (!userBean1.getUserName().equals("aaa") && userBean1 != null  ) {
//                return "success";
//            }
//        }catch (Exception e){
//            return "error";
//        }
    }

    @RequestMapping("findUserByItem")
    @RequiresPermissions("user:manage")
    public IPage<UserBean> findUserByItem(Integer pageNO,String username){
        return userService.findUserByItem(pageNO,username);
    }

    @RequestMapping("findUserByUserName")
    @RequiresPermissions("user:manage")
    public UserBean findUserByUserName(String username){
        return userService.findUserByUserName(username);
    }

    @RequestMapping("findAllGrade")
    @RequiresPermissions("user:manage")
    public List<GradeBean> findAllRole(){return userService.findAll();}

    @RequestMapping("findByRoleIds")
    @RequiresPermissions("user:manage")
    public  List<GradeBean> findByRoleIds(Integer[] roleIds){return userService.findByRoleIds(roleIds);}

    @RequestMapping("addUser")
    @RequiresPermissions("user:manage")
    public String addUser(UserBean userBean,Integer[] roleIds){
        UserBean user = userService.findUserByUserName("userBean.getUserName()");
        if (userBean.getUserName() == null){
            return "uerror";
        }
        if (user != null){
            return "u1";
        }
        if (userBean.getRealName() == null){
            return "rerror";
        }
        userService.addUser(userBean,roleIds);
        return "success";}

    @RequestMapping("delUser")
    @RequiresPermissions("user:manage")
    public String delUser(Integer userId){userService.delUser(userId);return "success";}

    @RequestMapping("findByUserId")
    @RequiresPermissions("user:manage")
    public UserBean findByUserId(Integer userId ,HttpServletRequest request){
        if (userId == null) {
            UserBean userBean= (UserBean) request.getSession().getAttribute("loginUser");
            return  userService.findByUserId(userBean.getUserId());
        }
       return userService.findByUserId(userId);
    }

    @RequestMapping("updateRoleByUserId")
    @RequiresPermissions("user:manage")
    public String updateRoleByUserId(Integer userId,Integer[] roleIds){
        userService.updateRoleByUserId(userId,roleIds);return "success";
    }

    @RequestMapping("updatePwd")
    @RequiresPermissions("user:manage")
    public String updatePwd(String newPassword,HttpServletRequest request){
        UserBean userBean= (UserBean) request.getSession().getAttribute("loginUser");
        userService.updatePwd(userBean.getUserId(),newPassword);
        return "success";
    }

}
